package com.airycode.distribution.distribution1.controller;

import com.airycode.distribution.distribution1.domain.MemberEntity;
import com.airycode.distribution.distribution1.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 会员管理
 * Created by zkr-fanxirui on 2017/8/14.
 */
@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/member-list")
    public String memberList(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        String resultPage = "";
        Map<String,Object> dataMap = new HashMap<String,Object>();
        dataMap = memberService.getMemberAll();
        resultMap.put("dataMap",dataMap);
        request.setAttribute("resultMap",resultMap);
        resultPage = "member/member-list";
        return resultPage;
    }


    //添加数据到数据库中
    @RequestMapping(value = "/memberAdd",method = RequestMethod.POST)
    public String memberAdd(HttpServletRequest request, HttpServletResponse response){
        String resultPage = "";
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> dataMap = new HashMap<String,Object>();
        String memberName = request.getParameter("memberName");
        String memberSex = request.getParameter("memberSex");
        String memberPhone = request.getParameter("memberPhone");
        String memberMail = request.getParameter("memberEmail");
        String memberAddress = request.getParameter("memberAddress");
        String beizhu = request.getParameter("beizhu");

        MemberEntity entity = new MemberEntity();
        entity.setId(UUID.randomUUID().toString());;
        entity.setMemberName(memberName);
        entity.setMemberSex(memberSex);
        entity.setMemberMail(memberMail);
        entity.setMemberPhone(memberPhone);
        entity.setMemberAddress(memberAddress);
        entity.setMemberJoinTime(new Date());


        //持久化到数据库中
        boolean flag = memberService.addMember(entity);
        if (flag) {
            resultPage = "redirect:/member-list";
        } else {
            //跳入到异常页面
        }

        return resultPage;
    }

    //根据id删除一个会员
    @RequestMapping(value = "/memberDelete")
    public String memberDelete(HttpServletRequest request, HttpServletResponse response){
        String resultPage = "";
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> dataMap = new HashMap<String,Object>();
        String id = request.getParameter("id");
        boolean flag = memberService.deleteMember(id);
        if (flag) {
            resultPage = "redirect:/member-list";
        } else {
            //跳入到异常页面
            resultPage = "redirect:/member-list";
        }
        resultMap.put("dataMap",dataMap);
        request.setAttribute("resultMap",resultMap);
        return resultPage;
    }


    //修改会员
    @RequestMapping(value = "/memberUpdate")
    @ResponseBody
    public MemberEntity memberUpdate(HttpServletRequest request, HttpServletResponse response){
        String resultPage = "";
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> dataMap = new HashMap<String,Object>();
        String id = request.getParameter("id");
        MemberEntity memberEntity = memberService.searchMember(id);
        return memberEntity;
    }

    //停用
    @RequestMapping(value = "/disableMember")
    public String disableMember(HttpServletRequest request, HttpServletResponse response){
        String resultPage = "";
        String id = request.getParameter("id");
        MemberEntity memberEntity = memberService.searchMember(id);
        int state=memberEntity.getMemberState()==1?0:1;
        memberEntity.setMemberState(state);
        //保存到數據庫中
        memberService.addMember(memberEntity);
        resultPage = "redirect:/member-list";
        return resultPage;
    }




}
